<?php

namespace app\shop\validate;

use app\shop\model\user\UserAddressModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserAddressValidate extends BaseValidate
{
    protected $rule = [
        'name'       => 'require',
        'phone'      => 'require|mobile',
        'province'   => 'require',
        'city'       => 'require',
        'county'     => 'require',
        'address'    => 'require',
        'is_default' => 'require|in:0,1',
        'id'         => 'require|exist'
    ];

    protected $message = [
        'name.require'       => '收货人姓名不能为空',
        'phone.require'      => '收货人手机号不能为空',
        'phone.mobile'       => '收货人手机号格式不正确',
        'province.require'   => '省份编码不能为空',
        'city.require'       => '城市编码不能为空',
        'county.require'     => '区县编码不能为空',
        'address.require'    => '详细地址不能为空',
        'is_default.require' => '默认地址不能为空',
        'is_default.in'      => '默认地址格式不正确',
        'id.require'         => '地址id不能为空',
        'id.exist'           => '地址信息不存在'
    ];

    protected $scene = [
        'list'   => [''],
        'detail' => ['id'],
        'add'    => ['name', 'phone', 'province', 'city', 'country', 'address', 'is_default'],
        'edit'   => ['id', 'name', 'phone', 'province', 'city', 'country', 'address'],
        'delete' => ['id']
    ];

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    protected function exist($value): bool
    {
        $address = UserAddressModel::where(['id' => $value])->select();

        return !$address->isEmpty();
    }
}
